package service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import bean.Files;
import bean.Folders;
import service.IFileService;
import utils.FileDataUtils;
import utils.FolderUtils;  // 导入工具类

public class IFileServiceImpl implements IFileService {

    private FileDataUtils fileDataUtils=new FileDataUtils();
    private FolderUtils folderUtils=new FolderUtils();
    
    private List<Files> allFileList=new ArrayList<Files>();
    private List<Folders> allFolderList=new ArrayList<Folders>();
    
    public IFileServiceImpl() {
        this.allFileList=fileDataUtils.loadData();  // 初始化时加载全部文件数据
        this.allFolderList=folderUtils.loadFolder();
    }
    
    @Override
    public List<Folders> getFoldersList(){
    	return allFolderList;
    }
    
    @Override
    public Folders getFileListById(String folderName,String folderId) {
    	Folders folders=new Folders();
    	List<Files> list=new ArrayList<Files>();
    	for(Files f:allFileList) {
    		if(folderId.equals(f.getFolderId())) {
    			list.add(f);
    		}
    	}
    	folders.setFilelist(list);
    	folders.setFolderName(folderName);
    	folders.setFolderId(folderId);
        return folders;  // 返回指定文件夹对象,包含了文件列表属性
    }
    
    @Override
    public Folders addFolder(String folderName) {
    	Folders folder=folderUtils.addFolder(folderName);
    	return folder;
    }

    @Override
    public void removeFolder(String folderId) {
    	Iterator<Files> iterator = allFileList.iterator();
    	while (iterator.hasNext()) {
    	    Files file = iterator.next();
    	    if (file.getFolderId().equals(folderId)) {
    	        iterator.remove(); // ✅ 安全删除
    	    }
    	}
    	
    	folderUtils.removeFolder(folderId);
    	return;
    }
    
    @Override
    public void updateFolder(String folderId,String folderName) {
    	folderUtils.updateFolder(folderId, folderName);
    	return ;
    }
    @Override
    public boolean addFile(Files file, String folderId) {
    	file.setFolderId(folderId);
        fileDataUtils.addFile(file);
        return true;  // 根据实际需求调整返回值
    }

    @Override
    public boolean deleteFile(String filepath) {//可修改为删除源文件
        return fileDataUtils.removeFile(filepath);
    }

    @Override
    public boolean removeFile(String filename) {//移出文件
        return fileDataUtils.removeFile(filename);  
    }

    @Override
    public boolean updateFile(String filename) {
        return fileDataUtils.updateFile(filename);
    }
    
    @Override
    public List<Files> searchFilesByName(String searchvalue){
    	return allFileList.stream()
                .filter(file -> file.getFileName() != null && 
                               file.getFileName().toLowerCase().contains(searchvalue.toLowerCase()))
                .collect(Collectors.toList());
    }

    @Override
    public boolean fileExist(String filename, String folderid) {
        return allFileList.stream()
                .anyMatch(file -> file.getFolderId().equals(folderid) && 
                                file.getFileName() != null && 
                                file.getFileName().equals(filename));
    }}